/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 */

import { Worker } from 'worker_threads';
import { PoolConstant } from '../../../../../src/base/internal/pool/constant/constant';
import { HWorker } from '../../../../../src/base/internal/pool/worker-manager/worker';
import { WorkerState } from '../../../../../src/base/internal/pool/enum/worker-state';
import { noop } from '../../../../../src/common/util/noop';
import { Work } from '../../../../../src/base/internal/pool/model/work';
import { Priority } from '../../../../../src/base/internal/pool/enum/priority';
import * as path from 'path';

describe('test hworker', () => {
  const worker: Worker = new Worker(path.resolve(__dirname, PoolConstant.WORKER_ACTION_PATH));
  const hWorker: HWorker = new HWorker(worker, 1);
  test('test setState', () => {
    hWorker.setState(WorkerState.IDLE);
    expect(hWorker.getState() === WorkerState.IDLE).toBeTruthy();
  });

  test('test setLastBusyTime', () => {
    const time:number=Date.now();
    hWorker.setLastBusyTime(time);
    expect(hWorker.getLastBusyTime() === time).toBeTruthy();
  });

  test('test doWork', () => {
    const work = new Work(Priority.HEAVY, undefined, '');
    hWorker.setState(WorkerState.BUSY);
    expect(hWorker.doWork(work)).toBeFalsy();
    hWorker.setState(WorkerState.IDLE);
    expect(hWorker.doWork(work)).toBeTruthy();
  });

  test('test terminate', () => {
    hWorker.terminate(noop);
    expect(hWorker.getState() === WorkerState.DEAD).toBeTruthy();
  });
  hWorker.terminate(noop);
});
